package com.pingan.license.interceptor;

import com.alibaba.fastjson.JSON;
import com.pingan.license.utils.LicenseVertify;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;


/**
 * @AUTHOR:LIUCHAO;
 * @DATE: 2020/12/1 19:58
 */
@Component
public class LicenseCheckInterceptor implements HandlerInterceptor {
    private static Logger logger = LogManager.getLogger(LicenseCheckInterceptor.class);


    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        LicenseVertify licenseVerify = new LicenseVertify();

        //校验证书是否有效
        boolean verifyResult = licenseVerify.verify();

        if(verifyResult){
            return true;
        }else{
            response.setCharacterEncoding("utf-8");
            Map result = new HashMap(1);
            result.put("result","您的证书无效，请核查服务器是否取得授权或重新申请证书！");

            response.getWriter().write(JSON.toJSONString(result));

            return false;
        }
    }

}